package com.xinli.xinlifm.view.adapter;

import android.support.v4.util.Pools;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.xinli.xinlifm.http.bean.DianTaiBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XiaoLiu on 2016/10/15.
 */

public class DianTaiPagerAdapter extends PagerAdapter {
    private List<DianTaiBean.DataBean.TuijianBean> entries;
    private Pools.Pool<ImageView> pool = new Pools.SimplePool<>(3);
    public DianTaiPagerAdapter(List<DianTaiBean.DataBean.TuijianBean> entries) {
        this.entries = entries;
        this.entries = new ArrayList<>(entries);
        DianTaiBean.DataBean.TuijianBean first = this.entries.get(0);
        DianTaiBean.DataBean.TuijianBean last = this.entries.get(entries.size() - 1);
        this.entries.add(0, last);
        this.entries.add(first);
    }

    @Override
    public int getCount() {
        return entries.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = pool.acquire();
        if (imageView == null) {
            imageView = new ImageView(container.getContext());
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        }
        Picasso.with(container.getContext()).load(entries.get(position).getCover()).into(imageView);
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        pool.release((ImageView) object);
    }
}
